import 'dart:async';
import 'dart:io';

import 'package:bugly_plugin/bugly_plugin.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutterdemo/common/global.dart';

import 'widgets/listview.dart';

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
//上报数据至Bugly
Future<Null> _reportError(dynamic error, dynamic stackTrace) async {
  BuglyPlugin.postException(error, stackTrace);
}

void main() {
  //注册Flutter框架的异常回调
  FlutterError.onError = (FlutterErrorDetails details) async {
    //转发至Zone的错误回调
    Zone.current.handleUncaughtError(details.exception, details.stack);
  };
  //自定义错误提示页面
  ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
    return Scaffold(
        body: Center(
      child: Text(
        "出现未知异常,请退出后重新打开应用。",
        style: TextStyle(
          color: Colors.red,
        ),
      ),
    ));
  };
  //使用runZone方法将runApp的运行放置在Zone中，并提供统一的异常回调
  runZoned<Future<Null>>(() async {
    WidgetsFlutterBinding.ensureInitialized();
    Global.init().then((value) => runApp(MyApp()));
  }, onError: (error, stackTrace) async {
    await _reportError(error, stackTrace);
  });

  if (Platform.isAndroid) {
    //设置Android头部的导航栏透明
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      systemNavigationBarColor: Colors.blue,
      systemNavigationBarDividerColor: Colors.transparent,
      //虚拟按键图标颜色
      systemNavigationBarIconBrightness: Brightness.light,
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
      //状态栏黑色
      statusBarIconBrightness: Brightness.light,
    ));
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    BuglyPlugin.setApp(androidAppId: "316058a814");
    return new MaterialApp(
      title: 'Flutter Demo',
      //主题
      theme: new ThemeData(
          primaryColor: Colors.orange,
          brightness: Brightness.light,
          backgroundColor: Colors.greenAccent),
      //应用首页路由
      home: new ListViewWidget(),
    );
  }
}
